package com.chengqs.rapid.core;

import com.chengqs.rapid.common.constants.RapidBufferHelper;
import com.chengqs.rapid.core.netty.NettyHttpClient;
import com.chengqs.rapid.core.netty.NettyHttpServer;
import com.chengqs.rapid.core.netty.processor.NettyBatchEventProcessor;
import com.chengqs.rapid.core.netty.processor.NettyCoreProcessor;
import com.chengqs.rapid.core.netty.processor.NettyMpmcProcessor;
import com.chengqs.rapid.core.netty.processor.NettyProcessor;
import lombok.extern.slf4j.Slf4j;

/**
 * <B>主类名称：</B>RapidContainer<BR>
 * <B>概要说明：</B>主流程的容器类<BR>
 *
 * @author: chengqs
 * @create: 2025-03-11 01:59:30
 **/
@Slf4j
public class RapidContainer implements LifeCycle {

    private RapidConfig rapidConfig; // 核心配置类

    private NettyHttpServer nettyHttpServer; // 接收http请求的server

    private NettyHttpClient nettyHttpClient; // http转发的核心类

    private NettyProcessor nettyProcessor; // 核心处理器

    public RapidContainer(RapidConfig rapidConfig) {
        this.rapidConfig = rapidConfig;
        init();
    }

    @Override
    public void init() {
        // 1. 构建核心处理器
        NettyCoreProcessor nettyCoreProcessor = new NettyCoreProcessor();

        // 2. 是否开启缓存
        String bufferType = rapidConfig.getBufferType();
        if (RapidBufferHelper.isFlusher(bufferType)) {
            nettyProcessor = new NettyBatchEventProcessor(rapidConfig, nettyCoreProcessor);
        } else if (RapidBufferHelper.isMpmc(bufferType)) {
            nettyProcessor = new NettyMpmcProcessor(rapidConfig, nettyCoreProcessor, true);
        } else {
            this.nettyProcessor = nettyCoreProcessor;
        }
        //	3. 创建NettyhttpServer
        nettyHttpServer = new NettyHttpServer(rapidConfig, nettyProcessor);

        // 4. 创建NettyHttpClient
        nettyHttpClient = new NettyHttpClient(rapidConfig, nettyHttpServer.getWorkEventLoopGroup());
    }

    @Override
    public void start() {
        nettyProcessor.start();
        nettyHttpServer.start();
        nettyHttpClient.start();
        log.info("RapidContainer started !");
    }

    @Override
    public void shutdown() {
        nettyProcessor.shutdown();
        nettyHttpServer.shutdown();
        nettyHttpClient.shutdown();
    }
}
